﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace Itop.GpsCar.Web.CustomAdmin
{
    public partial class checkcode : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            string checkCode = CreateCode(4);

            //用于验证 
            Session["CheckCode"] = checkCode;
            CreateImages(checkCode);
        }

        /*产生验证码*/
        public string CreateCode(int codeLength)
        {

            string so = "1,2,3,4,5,6,7,8,9,0";
            string[] strArr = so.Split(',');
            string code = "";
            Random rand = new Random();
            for (int i = 0; i < codeLength; i++)
            {
                code += strArr[rand.Next(0, strArr.Length)];
            }
            return code;
        }

        /*产生验证图片*/
        public void CreateImages(string code)
        {

            Bitmap image = new Bitmap(60, 20);
            Graphics g = Graphics.FromImage(image);
            WebColorConverter ww = new WebColorConverter();
            g.Clear((Color)ww.ConvertFromString("#ffffff"));

            Random random = new Random();
            //画图片的背景噪音线 
            for (int i = 0; i < 10; i++)
            {
                int x1 = random.Next(100);
                int x2 = random.Next(100);
                int y1 = random.Next(40);
                int y2 = random.Next(40);

                g.DrawLine(new Pen(Color.LightGray), x1, y1, x2, y2);
            }
            Font font = new Font("Arial", 12, FontStyle.Bold | FontStyle.Italic);
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
            new Rectangle(0, 0, image.Width, image.Height), Color.Black, Color.Gray, 1.2f, true);

            //画验证码字符串 
            for (int i = 0; i < code.Length; i++)
            {
                g.DrawString(code[i].ToString(), font, brush, i * 14 + 2, 2);
            }

            //画图片的前景噪音点 
            //for (int i = 0; i < 300; i++)
            //{
            //    int x = random.Next(image.Width);
            //    int y = random.Next(image.Height);
            //    image.SetPixel(x, y, Color.LightGray);
            //}

            //画图片的边框线 
            g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

            Response.Buffer = true;
            Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);
            Response.Expires = 0;
            Response.CacheControl = "no-cache";
            Response.AppendHeader("Pragma", "No-Cache");
            //将验证码图片写入内存流，并将其以 "image/Png" 格式输出 
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            try
            {
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                Response.ClearContent();
                Response.ContentType = "image/Png";
                Response.BinaryWrite(ms.ToArray());
            }
            finally
            {
                //显式释放资源 
                g.Dispose();
                image.Dispose();

            }
        }
    }
}
